#include "dvRegionPolygonItem.h"
#include "dvDefinitions.h"
#include "dvMapScene.h"

dvRegionPolygonItem::dvRegionPolygonItem(QGraphicsItem *parent) :
    QGraphicsPolygonItem()
{
    m_pen = QPen(Qt::black);
    setFlags(QGraphicsItem::ItemIsSelectable);
    m_brush = QBrush(QColor(Qt::white));
    m_font = QFont("Times New Roman",8);
    m_checked = false;
    m_enabled = false;

    m_pScene  = NULL;

    m_pTool = new PolygonTool(this);
}

dvRegionPolygonItem::~dvRegionPolygonItem()
{
    delete m_pTool;
}

void dvRegionPolygonItem::paint(QPainter *painter,
                                const QStyleOptionGraphicsItem *option,
                                QWidget *widget)
{
    QPen pen=m_pen;
    pen.setCosmetic(true);
    if (isChecked())
    {
        pen.setColor(QColor(Qt::blue));
        pen.setWidth(3);
    }
    painter->setPen(pen);
    painter->setBrush(m_brush);
    painter->drawPolygon(polygon());
    painter->setFont(m_font);
    //painter->drawText(polygon().boundingRect(), Qt::AlignCenter, data(OBJECT_NAME).toString());
}

void dvRegionPolygonItem::setPen(const QPen &pen)
{
    m_pen = pen;
    update();
}

void dvRegionPolygonItem::setBrush(const QBrush &brush)
{
    m_brush = brush;
    update();
}

bool dvRegionPolygonItem::loadObject(QDomNode node)
{
    QString value;
    value = node.attributes().namedItem("id").nodeValue();
    if (value.isEmpty())
    {
        return false;
    }
    setData(OBJECT_ID,QVariant(value));

    value = node.attributes().namedItem("name").nodeValue();
    if (value.isEmpty())
    {
        return false;
    }
    setData(OBJECT_NAME,QVariant(value));

    value = node.attributes().namedItem("color").nodeValue();
    if (value.isEmpty())
    {
        return false;
    }
    //setBrush(QBrush(dvMapScene::getColorRGBA(value)));
    setBrush(QBrush(QColor(Qt::white)));

    value = node.attributes().namedItem("zvalue").nodeValue();
    if (value.isEmpty())
    {
        return false;
    }
    setZValue(value.toDouble());

    value = node.attributes().namedItem("position").nodeValue();
    if (value.isEmpty())
    {
        return false;
    }
    QStringList position = value.split(":");
    if (position.count() != 2)
    {
        return false;
    }
    setPos(position.at(0).toDouble(),position.at(1).toDouble());

    value = node.attributes().namedItem("points").nodeValue();
    if (value.isEmpty())
    {
        return false;
    }
    QStringList points = value.split(";");

    QPolygonF polygon;
    int nodes = 0;
    foreach(QString point, points)
    {
        if (!point.isEmpty())
        {
            QStringList xy = point.split(":");
            if (xy.count() == 2)
            {
                QPointF pf(xy.at(0).toDouble(), xy.at(1).toDouble());
                polygon << pf;
                nodes++;
            }
        }
    }
    if (nodes > 1)
    {
        setPolygon(polygon);
        m_pTool->setPos(polygon.boundingRect().center());
        m_pTool->hide();
        m_pTool->setParentItem(this);
        m_pTool->setTextRegion(" " + data(OBJECT_NAME).toString());
        m_pTool->setTextIndicator("Indicator $: 1 000 000 000");
        return true;
    }
    return false;
}

void dvRegionPolygonItem::setChecked(bool state)
{
    m_checked = state;

    if (m_checked)
    {
        m_pTool->show();
    }
    else
    {
        m_pTool->hide();
    }

}

void dvRegionPolygonItem::setTextIndicator(const QString &text)
{
    m_pTool->setTextIndicator(text);
}

QVariant dvRegionPolygonItem::itemChange(QGraphicsItem::GraphicsItemChange change,
                                         const QVariant &value)
{
    if (change == ItemSelectedChange)
    {
        //setChecked(!isChecked());
    }
    return value;
}

void dvRegionPolygonItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (!isEnabled())
    {
        return;
    }

    if (event->button() == Qt::LeftButton)
    {
        setChecked(!isChecked());
        update();
        if (m_pScene)
        {
            m_pScene->sendPolygonChecked(data(OBJECT_ID), isChecked());
        }
    }
}

// PolygonTool !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

PolygonTool::PolygonTool(QGraphicsPolygonItem *poly)
{
    setFlags(ItemIgnoresTransformations);
    m_pen = QPen(Qt::black);
    m_brush = QBrush(QColor(255,255,255,200));
    m_font = QFont("Times New Roman", 12);
    setTextRegion("Region");
    setTextIndicator("Indicator: 1000000000000");
    setZValue(1000);
}

PolygonTool::~PolygonTool()
{
}

QRectF PolygonTool::boundingRect() const
{
    const int Margin = 1;
    return m_contentRect.adjusted(-Margin, -Margin, +Margin, +Margin);
}

QPainterPath PolygonTool::shape() const
{
    QPainterPath path;
    path.addRect(m_contentRect);
    return path;
}

void PolygonTool::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QPen pen(m_pen);
    pen.setWidth(2);
    painter->setPen(pen);
    painter->setBrush(m_brush);
    painter->setFont(m_font);
    painter->drawRect(m_contentRect);
    painter->drawText(m_contentRect, Qt::AlignTop | Qt::AlignLeft, m_textRegion);
    painter->drawText(m_contentRect, Qt::AlignBottom | Qt::AlignLeft, m_textIndicator);
    painter->drawLine(0, -m_contentRect.height(), 0, 0);
    painter->drawEllipse(-5, -5, 10, 10);
}

void PolygonTool::setTextRegion(const QString &text)
{
    m_textRegion = text;
    correctContentRect();
}

void PolygonTool::setTextIndicator(const QString &text)
{
    m_textIndicator = text;
    correctContentRect();
}

void PolygonTool::correctContentRect()
{
    QFontMetricsF fm   = QFontMetricsF(m_font);
    qreal letterWidth  = fm.width("1");
    qreal letterHeight = fm.height();

    int   lettersCount = 0;
    if (m_textRegion.length() > m_textIndicator.length())
        lettersCount = m_textRegion.length();
    else
        lettersCount = m_textIndicator.length();

    lettersCount += 1;

    qreal rectWidth    = lettersCount * letterWidth;
    qreal rectHeight   = 2 * letterHeight;

    m_contentRect.setLeft(-rectWidth / 2);
    m_contentRect.setTop(-rectHeight*2);
    m_contentRect.setWidth(rectWidth);
    m_contentRect.setHeight(rectHeight);
}











































